/**
 * 完结申请
 * Created by xc on 2015/6/17.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('CostReportCtrl', CostReportCtrl);
    CostReportCtrl.$inject = ['MHService', 'Upload'];
    function CostReportCtrl(MHService, Upload) {
      var costReport = this;
      var imgUrl = [];//图片路径
//完结申请
      costReport.costreportApply = function () {
        if (!costReport.description) {
          MHService.Toast('请填写申请说明');
          return;
        }
        var gardenid = MHService.globalData(false, {key: 'projectId'}),//项目id
          processid = MHService.globalData(false, {key: 'processId'}),//工序id
          procedureid = MHService.globalData(false, {key: 'procedureid'});//流程id
        var path = MHService.URL.costreport + '?gardenid=' + gardenid + '&proid=' + procedureid + '&processid=' + processid;
        var _req = new FormData();
        _req.append('content', costReport.description);
        _req.append('attchs', imgUrl);

        $.ajax({
          type: 'POST',
          url: path,
          data: _req,
          processData: false,
          contentType: false,
          complete: function (data) {
            console.log(data.responseText);
            if (data.responseText.match('"success":true')) {
              MHService.Toast('提交成功');
              history.go(-2);
            } else {
              MHService.Toast('提交失败，请刷新重试');
            }
          }
        });
      };
      /**
       * 选中了文件
       * @param $files
       */
      costReport.onFileSelect = function ($files) {
        if ($files.length > 0) {
          Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: $files[0]}).success(function (res) {
            //文件上传完成
            console.log(res);
            imgUrl.push(res.data);
            //$scope.images.push('../' + res.data.path);
            //imgUrl  用于存储图片路径的变量，这里要根据后台需要的参数格式格式化后赋值给imgUrl
          });
        }
      };
    }
  }
});
